package com.qiguliuxing.dts.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.service.base.DtsKeywordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsKeyword;

@RestController
@RequestMapping("/admin/keyword")
@Validated
@Api(tags = ApiAdminConsts.Keyword.PARENT)
public class AdminKeywordController {
	private static final Logger logger = LoggerFactory.getLogger(AdminKeywordController.class);

	@Autowired
	private DtsKeywordService keywordService;

	@RequiresPermissions("admin:keyword:list")
	@RequiresPermissionsDesc(menu = { "商场管理", "关键词" }, button = "查询")
	@GetMapping("/list")
	@ApiOperation(value=ApiAdminConsts.Keyword.LIST)
	@WebLog(description = ApiAdminConsts.Keyword.LIST,excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "关键字") String keyword,
					   @ApiParam(value = "链接url") String url,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
		List<DtsKeyword> brandList = keywordService.querySelective(keyword, url, page, limit, sort, order);
		long total = PageInfo.of(brandList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("items", brandList);
		return ResponseUtils.ok(data);
	}

	private Object validate(DtsKeyword keywords) {
		String keyword = keywords.getKeyword();
		if (StringUtils.isEmpty(keyword)) {
			return ResponseUtils.badArgument();
		}
		String url = keywords.getUrl();
		if (StringUtils.isEmpty(url)) {
			return ResponseUtils.badArgument();
		}
		return null;
	}

	@RequiresPermissions("admin:keyword:create")
	@RequiresPermissionsDesc(menu = { "商场管理", "关键词" }, button = "添加")
	@ApiOperation(value=ApiAdminConsts.Keyword.CREATE)
	@PostMapping("/create")
	public Object create(@RequestBody DtsKeyword keywords) {
		Object error = validate(keywords);
		if (error != null) {
			return error;
		}
		keywordService.add(keywords);
		return ResponseUtils.ok(keywords);
	}

	@RequiresPermissions("admin:keyword:read")
	@RequiresPermissionsDesc(menu = { "商场管理", "关键词" }, button = "详情")
	@ApiOperation(value=ApiAdminConsts.Keyword.READ)
	@GetMapping("/read")
	public Object read(@ApiParam(value = "关键词信息id") @NotNull Integer id) {
		DtsKeyword keywords = keywordService.findById(id);
		return ResponseUtils.ok(keywords);
	}

	@RequiresPermissions("admin:keyword:update")
	@RequiresPermissionsDesc(menu = { "商场管理", "关键词" }, button = "编辑")
	@ApiOperation(value=ApiAdminConsts.Keyword.UPDATE)
	@PostMapping("/update")
	public Object update(@RequestBody DtsKeyword keywords) {
		Object error = validate(keywords);
		if (error != null) {
			return error;
		}
		if (keywordService.updateById(keywords) == 0) {
			logger.info("商场管理->关键词->编辑 错误：{}", "更新数据失败！");
			return ResponseUtils.updatedDataFailed();
		}
		return ResponseUtils.ok(keywords);
	}

	@RequiresPermissions("admin:keyword:delete")
	@RequiresPermissionsDesc(menu = { "商场管理", "关键词" }, button = "删除")
	@ApiOperation(value=ApiAdminConsts.Keyword.DELETE)
	@PostMapping("/delete")
	public Object delete(@RequestBody DtsKeyword keyword) {
		Integer id = keyword.getId();
		if (id == null) {
			return ResponseUtils.badArgument();
		}
		keywordService.deleteById(id);
		return ResponseUtils.ok();
	}
}
